<template>
    <div :class="className" :style="{height:height,width:width}" />
  </template>
  
  <script>
  import * as echarts from 'echarts';
  require('echarts/theme/macarons'); // echarts theme
  import resize from '@/views/dashboard/mixins/resize';
  import axios from 'axios';
  import { getStatisticsId } from '@/api/questionnaire/statistics';
  
  const animationDuration = 6000
  
  export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    questionnaireId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      statisticsData: []
    };
  },
  watch: {
    questionnaireId: {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.fetchStatistics();
        }
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    async fetchStatistics() {
      try {
        const response = await getStatisticsId(this.questionnaireId);
        this.statisticsData = response;
        console.log('Fetched statistics data:', this.statisticsData); // 调试信息
        this.$nextTick(() => {
          this.initChart();
        });
      } catch (error) {
        console.error('Error fetching statistics:', error);
      }
    },
    
    initChart() {
      if (this.$el) {
        this.chart = echarts.init(this.$el, 'macarons');
        console.log('ECharts instance created:', this.chart); // 调试信息

        const option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: 10,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: this.statisticsData.map(item => item.optionText),
            axisTick: {
              alignWithLabel: true
            }
          }],
          yAxis: [{
            type: 'value',
            axisTick: {
              show: false
            }
          }],
          series: [
            {
              name: '响应次数',
              type: 'bar',
              stack: 'vistors',
              barWidth: '60%',
              data: this.statisticsData.map(item => item.responseCount),
              animationDuration
            },
            {
              name: '百分比',
              type: 'line',
              data: this.statisticsData.map(item => item.percentage),
              animationDuration
            }
          ]
        };

        this.chart.setOption(option);
      } else {
        console.error('Element not found for ECharts initialization'); // 调试信息
      }
    }
  }
};
</script>

<style scoped>
.chart {
  width: 100%;
  height: 400px;
}
</style>